package cn.imu.appdevexperiment2020.cch.cchactivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.cch.cchactivity.fragment.CchMainActivity;
import cn.imu.appdevexperiment2020.cch.retrofit.api.ApiClient;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.LoginResultBean;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.LoginSendBean;
import cn.imu.appdevexperiment2020.cch.util.AuthUtil;
import cn.imu.appdevexperiment2020.cch.util.ToastUtil;
import cn.imu.appdevexperiment2020.common.BaseActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-05 00:51
 */
public class LoginTestActivity extends BaseActivity {

   private static final String TAG = "LoginTestActivity";
   @BindView(R.id.til_login_username)
   TextInputLayout tilLoginUsername;
   @BindView(R.id.til_login_password)
   TextInputLayout tilLoginPassword;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.cch_activity_login);
   }

   @OnClick(R.id.bt_login)
   public void login(){

      String username=tilLoginUsername.getEditText().getText().toString();
      String password=tilLoginPassword.getEditText().getText().toString();

      LoginSendBean loginSendBean =new LoginSendBean();

      loginSendBean.setUserName(username);
      loginSendBean.setPassword(password);

      Call<LoginResultBean> call = ApiClient.getInstance(getApplicationContext()).getReturnData(AuthUtil.GetAuth(),loginSendBean);

      call.enqueue(new Callback<LoginResultBean>() {
         @Override
         public void onResponse(Call<LoginResultBean> call, Response<LoginResultBean> response) {
            if (null!=response.body()){

               if (200==response.body().getCode()){
                  Log.e(TAG,response.body().getMsg());
                  Log.e(TAG,response.body().getNickName());
                  SharedPreferences sharedPreferences = getSharedPreferences("cch", Context.MODE_PRIVATE);
                  SharedPreferences.Editor editor = sharedPreferences.edit();

                  editor.putString("nickname", response.body().getNickName());
                  editor.putString("username", response.body().getUserName());
                  editor.putString("classesId", String.valueOf(response.body().getClassesId()));
                  editor.putString("userId", String.valueOf (response.body().getUserId()));
                  editor.apply();
                  ToastUtil.ShowToast(getApplicationContext(),"登陆成功");
                  Intent intent = new Intent(LoginTestActivity.this, CchMainActivity.class);
                  startActivity(intent);
                  finish();
               }else{

               }
            }else{

            }
         }

         @Override
         public void onFailure(Call<LoginResultBean> call, Throwable t) {

         }
      });



   }
   @OnClick(R.id.tv_register)
   public void register(){
      Intent intent =new Intent(LoginTestActivity.this,RegisterTestActivity.class);
      startActivity(intent);
   }

}
